package com.jhws.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jhws.business.device.bo.DeviceConstant;
import org.apache.commons.lang.StringUtils;

import com.jhws.common.base.bo.Global;

public class AccountsUtils {
    private static StringBuffer stringBuffer;

    //从99到36进制9a, 中间要越过一部分数字
    private static int gap = 234;

    //将超过2位数的AreaCode转换成对应长度的36进制
    public static String getCompactAeraCode(String code) {

        Integer icode = Integer.valueOf(code);
        if (icode < 99)
            return code;
        String result = Long.toString(icode + gap, 36);
        return result.substring(0, 2);
    }

    //返回AreaCode的十进制
    public static String getDeciAreaCode(String code) {
        if (code.length() == 2 && (code.charAt(0) >= 'A' || code.charAt(1) >= 'A')) {
            Integer ret = Integer.valueOf(code, 36) - gap;
            return ret.toString();
        } else
            return code;
    }

    // 通过帐号返回对应的楼宇
    public static String getBuildingInfo(String roomNum) {
        if (roomNum.length() == 12) {
            stringBuffer = new StringBuffer();
            stringBuffer.append(roomNum.substring(4, 6)).append("栋").append(roomNum.substring(6, 8)).append("单元")
                    .append(roomNum.substring(8, 10)).append("楼").append(roomNum.substring(10, 12)).append("房间");
        }
        return stringBuffer.toString();
    }

    // 通过帐号返回对应的单元信息
    public static String getUnitInfo(String roomNum) {
        stringBuffer = new StringBuffer();
        stringBuffer.append(roomNum.substring(4, 6)).append("栋").append(roomNum.substring(6, 8)).append("单元");
        return stringBuffer.toString();
    }

    public static String getAreaNameFromRoomInfo(String roomInfo) {
        if (StringUtils.isEmpty(roomInfo)) {
            return null;
        }
        String[] infos = roomInfo.split(" ");
        if (infos.length > 1)
            return infos[1];
        else
            return null;
    }

    public static String getBuildingNameFromRoomInfo(String roomInfo) {
        if (StringUtils.isEmpty(roomInfo)) {
            return null;
        }
        String[] infos = roomInfo.split(" ");
        if (infos.length > 2)
            return infos[2];
        else
            return null;
    }

    // 获取小区编号
    public static String getCommCode(String roomNum) {
        if (devicesType(roomNum)!=DeviceConstant.VOICE_GATEWAY_TYPE && (StringUtils.isEmpty(roomNum) || roomNum.length() != 12)) {
            return "";
        }
        return roomNum.substring(0, 4);
    }

    // 获取楼栋
    public static String getBuilding(String roomNum) {
        if (StringUtils.isEmpty(roomNum) || roomNum.length() != 12) {
            return "";
        }
        return roomNum.substring(4, 6);
    }

    // 获取单元
    public static String getUnit(String roomNum) {
        if (StringUtils.isEmpty(roomNum) || roomNum.length() != 12) {
            return "";
        }
        return roomNum.substring(6, 8);
    }

    // 获取楼层
    public static String getFloor(String roomNum) {
        if (StringUtils.isEmpty(roomNum) || roomNum.length() != 12) {
            return "";
        }
        return roomNum.substring(8, 10);
    }

    // 获取房间号
    public static String getRoom(String roomNum) {
        if (roomNum.length() != 12 || StringUtils.isEmpty(roomNum)) {
            return null;
        }
        return roomNum.substring(10, 12);
    }

    // 根据帐号判断类型
    public static int devicesType(String sipNum) {
        sipNum = replaceBlank(sipNum);
        if (sipNum != null && sipNum.length() == 12) {
            String devicesType = sipNum.substring(8, 11);
            if ("000".equals(devicesType)) {
                devicesType = sipNum.substring(4, 11);
                if ("0000000".equals(devicesType)) {
                    if (isLetter(sipNum)) {
                        return DeviceConstant.WALL_TYPE;
                    } else {
                        return DeviceConstant.MANAGE_TYPE;
                    }
                } else {
                    return DeviceConstant.OUTDOOR_TYPE;
                }
            } else {
                return DeviceConstant.INDOOR_TYPE;
            }
        }
        else if(sipNum.endsWith("PstnServer"))
            return DeviceConstant.VOICE_GATEWAY_TYPE;
        else if (sipNum != null && sipNum.length() == 10) {
            try {
                Long i = Long.parseLong(sipNum, 36);
                return DeviceConstant.VILLA_TYPE;
            } catch (Exception e) {
                e.printStackTrace();
                return DeviceConstant.NOT_CONFIG_TYPE;
            }
        } else {
            return DeviceConstant.NOT_CONFIG_TYPE;
        }
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    public static boolean isLetter(String str) {
        //
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("([a-z]+\\d+)|(\\d+[a-z]+)");
        java.util.regex.Matcher m = pattern.matcher(str);
        return m.matches();
    }

    public static String appendZeroBuilding(String buildingCode) {
        if (Integer.valueOf(buildingCode) < 10) {
            buildingCode = "0" + Integer.valueOf(buildingCode);
        } else {
            buildingCode = String.valueOf(Integer.valueOf(buildingCode));
        }
        return buildingCode;
    }


    /**
     * 判断传入的字符串是否电话号码
     *
     * @param str
     * @return
     */
    public static boolean isChinaPhoneLegal(String str) {
        String regExp = "^1\\d{10}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
